<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="400" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" style="width: 90%">
<!--			<el-form-item label="选择角色" prop="parentId">-->
<!--				<el-cascader v-model="form.stuffNumber" :options="groups" :props="groupsProps" :show-all-levels="false" clearable style="width: 100%;"></el-cascader>-->
<!--			</el-form-item>-->
			<el-select v-model="form.roleIdList" multiple placeholder="请选择角色" style="width: 100%;">
				<el-option
					v-for="item in options"
					:key="item.id"
					:label="item.roleName"
					:value="item.id"
				/>
			</el-select>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "send",
				titleMap: {
					send: '选择角色'
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
					staffId: '',
					roleIdList: []
				},
				//验证规则
				rules: {
					name: [
						{required: true, message: '请输入姓名'}
					]
				},
				options: [],
				//所需数据选项
				groupsProps: {
				    label: "label",
					value: "value",
					emitPath: false,
					checkStrictly: false
				},
                groups: [
                    {
                        value: '1',
                        label: '观音桥校区',
                        children: [
                            {
                                value: 'disciplines',
                                label: '李老师'
                            },
                            {
                                value: 'navigation',
                                label: '张老师'
                            },
                        ],
                    },
                    {
                        value: '2',
                        label: '石桥铺校区',
                        children: [
                            {
                                value: 'basic',
                                label: '刘老师'
                            },
                            {
                                value: 'form',
                                label: '王老师'
                            }
                        ],
                    },
                    {
                        value: '3',
                        label: '南坪校区',
                        children: [
                            {
                                value: 'axure',
                                label: '陈老师',
                            },
                            {
                                value: 'sketch',
                                label: '吴老师',
                            }
                        ],
                    },
                ]
			}
		},

		mounted() {
			this.getRoles()
		},
		methods: {
			async getRoles(){

			},
			//显示
			open(mode='send'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			async submit(){
				var valid = await this.$refs.dialogForm.validate().catch(() => {})
				if(!valid){
					return false
				}
				this.isSaveing = true;
                var res = await this.$API.user.user.addRole.post(this.form);
				this.isSaveing = false;
				if(res.code == 0){
					this.$emit('success', this.form, this.mode)
					this.visible = false;
					this.$message.success("操作成功")
				}else{
					this.$alert(res.message, "提示", {type: 'error'})
				}
			},
			//表单注入数据
			setData(data, o){
				this.form.staffId = data.id
				this.form.roleIdList = data.roleIdList
				this.options = o
				//可以和上面一样单个注入，也可以像下面一样直接合并进去
				//Object.assign(this.form, data)
			}
		}
	}
</script>

<style>
</style>
